package com.ky.facescan.base;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import android.os.SystemClock;

import com.alibaba.fastjson.JSONObject;
import com.alipay.zoloz.smile2pay.InstallCallback;
import com.alipay.zoloz.smile2pay.Zoloz;
import com.alipay.zoloz.smile2pay.ZolozConnectCallback;
import com.alipay.zoloz.smile2pay.detect.DetectCallback;
import com.alipay.zoloz.smile2pay.verify.FaceVerifyResult;
import com.alipay.zoloz.smile2pay.verify.Smile2PayResponse;
import com.ky.facescan.IsvConfig;
import com.ky.facescan.bean.MyEvent;
import com.ky.facescan.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;

public class SmileManager {
    private static SmileManager sSmileManager = null;

    private final InstallCallback mInstallCallback = new InstallCallback() {
        public void onResponse(Smile2PayResponse smile2PayResponse) {
            LogUtils.e("Hello", "onResponse == > " + smile2PayResponse.getCode());
            LogUtils.e("Hello", "onResponse == > " + smile2PayResponse.getSubMsg());
            if (smile2PayResponse.getCode() == 1000) {
                EventBus.getDefault().postSticky(new MyEvent("event_smile_install_finish", true));
            } else {
                EventBus.getDefault().postSticky(new MyEvent("event_smile_install_finish", false));
            }

        }
    };
    private final DetectCallback mDetectScanCallback = new DetectCallback() {
        long triggerTime = 0L;

        protected void onTriggerEvent(int eventCode, boolean trigger, Map<String, Object> extInfo) {
            super.onTriggerEvent(eventCode, trigger, extInfo);
            if (trigger) {
                if (System.currentTimeMillis() - this.triggerTime < 3000L) {
                    return;
                }

                this.triggerTime = System.currentTimeMillis();
                EventBus.getDefault().post(new MyEvent("event_smile_detect"));
            }

        }

        protected void onFaceVerify(Smile2PayResponse smile2PayResponse) {
            super.onFaceVerify(smile2PayResponse);
            LogUtils.e("Hello", "onFaceVerify == > " + JSONObject.toJSONString(smile2PayResponse.getCode()));
            LogUtils.e("Hello", "onFaceVerify == > " + JSONObject.toJSONString(smile2PayResponse.getSubMsg()));
            if (smile2PayResponse.getCode() == 1000) {
                FaceVerifyResult faceVerifyResult = (FaceVerifyResult) smile2PayResponse.getExtInfo().get("faceVerifyResult");
                EventBus.getDefault().post(new MyEvent("event_smile_verify_success", faceVerifyResult));
            } else if (smile2PayResponse.getCode() == 1001) {
                EventBus.getDefault().post(new MyEvent("event_smile_verify_failed", "在线比对失败"));
            } else {
                EventBus.getDefault().post(new MyEvent("event_smile_verify_failed", "比对失败"));
            }

        }

        protected void onError(String s, String s1) {
            LogUtils.e("Hello", "onError == > " + s);
            LogUtils.e("Hello", "onError == > " + s1);
        }
    };
    private final ZolozConnectCallback mZolozConnectCallback = (b, componentName) -> {
        LogUtils.e("Hello", "Smile 服务连接情况 == > " + b);
        if (!b) {
            EventBus.getDefault().post(new MyEvent("event_smile_disconnect"));
        }

    };
    private Zoloz mZoloz;

    private SmileManager() {
    }

    public static SmileManager getInstance() {
        Class var0 = SmileManager.class;
        synchronized (SmileManager.class) {
            if (sSmileManager == null) {
                sSmileManager = new SmileManager();
            }

            return sSmileManager;
        }
    }

    public synchronized void init() {
        LogUtils.e("Hello", "init == > call");
        this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
        EventBus.getDefault().postSticky(new MyEvent("event_smile_install_start"));
//        this.mZoloz.install(this.getInstallParams());
        this.mZoloz.register(null, this.mInstallCallback);
        this.mZoloz.setConnectCallback(this.mZolozConnectCallback);
    }

    public synchronized void detect() {
        LogUtils.e("Hello", "detect == > call");
        FaceFrame.sFtp.execute(() -> {
            this.cancelDetect();
            SystemClock.sleep(500L);
            this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
            this.mZoloz.detect(mockDetectInfo(), this.mDetectScanCallback);
        });
    }

    public synchronized void cancelDetect() {
        LogUtils.e("Hello", "cancelDetect == > call");
        this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
        this.mZoloz.detect((String) null, (Map) null, (DetectCallback) null);
    }


    public synchronized void continueDetect() {
        LogUtils.e("Hello", "continueDetect == > call");
        Map<String, Object> command = new HashMap(2);
        command.put("commandCode", 3);
        this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
        this.mZoloz.command(command);
    }

    public synchronized void exitSmile() {
        LogUtils.e("Hello", "exitSmile == > call");
        HashMap<String, Object> command = new HashMap();
        command.put("commandCode", 0);
        this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
        this.mZoloz.command(command);
    }

    public synchronized void release() {
        this.mZoloz = Zoloz.getInstance(FaceFrame.getAppContext());
        this.mZoloz.detect((String) null, (Map) null, (DetectCallback) null);
        this.mZoloz = null;
        sSmileManager = null;
    }

//    private synchronized Map<String, Object> getInstallParams() {
//        HashMap<String, Object> params = new HashMap();
//        params.put("isvPid", IsvConfig.ISV_PID);
//        params.put("isvName", IsvConfig.ISV_NAME);
//        params.put("merchantId", IsvConfig.MERCHANT_ID);
//        params.put("merchantName", IsvConfig.MERCHANT_NAME);
//        params.put("groupID", IsvConfig.GROUP_ID);
//        params.put("merchantPayPid", IsvConfig.MERCHANT_PAY_PID);
//        return params;
//    }

    private HashMap<String, Object> mockDetectInfo() {
        JSONObject configInfo = new JSONObject();
        configInfo.put("faceMode", "entrance");
        configInfo.put("toygerPowerMode", 0);
        configInfo.put("cameraID", 0);
        configInfo.put("displayAngle", 0);
        configInfo.put("isDisplayMirror", true);
        configInfo.put("isMirror", false);
        configInfo.put("supIsDisplayMirror", false);
        configInfo.put("supIsMirror", false);
        HashMap<String, Object> zolozConfig = new HashMap();
        zolozConfig.put("riskDecision", 2);
        zolozConfig.put("zolozConfig", configInfo.toJSONString());
        zolozConfig.put("only_detect_entry", true);
        zolozConfig.put("ladyBirdEnable", true);
        zolozConfig.put("needFaceRegion", true);
        return zolozConfig;
    }
}
